//
//  CourseThreeViewController.m
//  IhuayuanClient
//
//  Created by 黄思清 on 2017/7/3.
//  Copyright © 2017年 com.ihuayuan. All rights reserved.
//

#import "CourseThreeViewController.h"
#import "AdjustHeight.h"
#import "CourseThreeViewTableViewCell.h"

@interface CourseThreeViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger page;
}
@property(nonatomic,strong)UITableView *ListV;
@property(nonatomic,strong)NSMutableArray *ListArr;
@property (nonatomic,strong) NSMutableDictionary *heightDict;
@property(nonatomic,strong) NSMutableArray *requestArr;
@end

@implementation CourseThreeViewController
-(NSMutableArray *)requestArr
{
    if (!_requestArr) {
        _requestArr = [NSMutableArray array];
    }
    return _requestArr;
}
-(NSMutableArray *)ListArr
{
    if (!_ListArr) {
        _ListArr = [NSMutableArray array];
    }
    return _ListArr;
}

-(UITableView *)ListV
{
    if (!_ListV) {
        _ListV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH-200) style:(UITableViewStyleGrouped)];
        _ListV.delegate = self;
        _ListV.dataSource = self;
        _ListV.backgroundColor = [UIColor whiteColor];
        _ListV.separatorStyle = 0;
        _ListV.estimatedSectionHeaderHeight = 0;
        _ListV.estimatedSectionFooterHeight = 0;
        [_ListV registerClass:[CourseThreeViewTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CourseThreeViewTableViewCell class])];
    }
    return _ListV;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    [self.view setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:self.ListV];
    [self Request];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (self.requestArr.count == 0)
    {
        self.ListV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"no_moreData"]];
    }else
    {
        self.ListV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];

    }
    return self.requestArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *modelDic  = self.requestArr[indexPath.row];
    CourseThreeViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CourseThreeViewTableViewCell" forIndexPath:indexPath];
    cell.titlel.text = modelDic[@"teacher_name"];
    cell.timeL.text = modelDic[@"addtime"];
    cell.contents.text = modelDic[@"contents"];
    cell.imageArr = modelDic[@"thumbs"];
    
    cell.selectionStyle = 0;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.requestArr[indexPath.row];
    NSArray *listArr = dic[@"thumbs"];



    if (listArr.count == 0)
    {
        return 70+[AdjustHeight adjustHeightByString:dic[@"contents"] width:KscreenW-20 font:17];
    }else
    {
        return 70+[AdjustHeight adjustHeightByString:dic[@"contents"] width:KscreenW-20 font:17]+(listArr.count+1)/2*(KscreenW-30)/2;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FLT_MIN;
}



#pragma mark - 网络请求
- (void)Request
{
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=StudentCourse&a=courseReport" parmDic:@{@"aid":self.aid,@"page":[NSString stringWithFormat:@"%ld",page]} requestType:RequestGET Success:^(id data)
     {
         NSMutableArray *dataArr = [data mutableCopy];

         if (page == 1)
         {
             [self.requestArr removeAllObjects];
             [self.ListArr removeAllObjects];
         }

         if (dataArr.count==0)
         {
             page--;
         }

         [self.requestArr addObjectsFromArray:dataArr];

         [self.ListV.mj_footer endRefreshing];
         [self.ListV.mj_header endRefreshing];

         if (dataArr.count == 0||dataArr.count<10)
         {
             self.ListV.mj_footer.state = MJRefreshStateNoMoreData;
         }

         [self.ListV reloadData];
     } error:^(NSError *error) {

     }];

    [self.ListV reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
